// *****************************************************
// Aitrox Information Technology
// http://www.proxima-ai.com
// Copyright 2021 Aitrox. All rights reserved.
//
// Filename: heart.go
// CreateDate: 2023-05-08
// Author: zjp
// Description: 获取心脏测量数据
// ******************************************************
package export

import (
	"fmt"

	"github.com/jinzhu/gorm"
)

type HeartMeasureInfo struct {
	*NidusExtInfo
	ChamberType  int     `json:"chamberType" gorm:"Column:chamberType"`
	ChamberProb  float32 `json:"chamberProb" gorm:"Column:chamberProb"`
	LVEF         float32 `json:"LVEF" gorm:"Column:LVEF"`
	LongDiameter float32 `json:"longDiameter" gorm:"Column:longDiameter"`
	Area         float32 `json:"area" gorm:"Column:area"`
	Volume       float32 `json:"volume" gorm:"Column:volume"`
	Period       int     `json:"period" gorm:"Column:period"`
}

type HeartMeasureData struct {
	PatientId   int     `json:"patientId" gorm:"Column:patientId"`
	ChamberType int     `json:"chamberType" gorm:"Column:chamberType"`
	CheckedType int     `json:"checkedType" gorm:"Column:checkedType"`
	LVEF        float32 `json:"LVEF" gorm:"Column:LVEF"`
	LVEFSimpson float32 `json:"LVEFSimpson" gorm:"Column:LVEFSimpson"`
}

type ChamberMeasureData struct {
	PatientId   int     `json:"patientId" gorm:"Column:patientId"`
	ChamberType int     `json:"chamberType" gorm:"Column:chamberType"`
	CheckedType int     `json:"checkedType" gorm:"Column:checkedType"`
	AoDiameter  float32 `json:"aoDiameter" gorm:"Column:aoDiameter"`
	Ivst        float32 `json:"ivst" gorm:"Column:ivst"`
	LaDiameter  float32 `json:"laDiameter" gorm:"Column:laDiameter"`
	LvDiameter  float32 `json:"lvDiameter" gorm:"Column:lvDiameter"`
	Lvpwt       float32 `json:"lvpwt" gorm:"Column:lvpwt"`
	Area        float32 `json:"area" gorm:"Column:area"`
	Padd        float32 `json:"padd" gorm:"Column:padd"`
	Period      int     `json:"period" gorm:"Column:period"`
	ImageType   int     `json:"imageType" gorm:"Column:imageType"`
}

type WaveformMeasureData struct {
	PatientId   int     `json:"patientId" gorm:"Column:patientId"`
	Period      int     `json:"period" gorm:"Column:period"`
	VelE        float32 `json:"velE" gorm:"Column:velE"`
	VelA        float32 `json:"velA" gorm:"Column:velA"`
	Velep       float32 `json:"velep" gorm:"Column:velep"`
	TDIS        float32 `json:"TDIS" gorm:"Column:TDIS"`
	TAPSE       float32 `json:"TAPSE" gorm:"Column:TAPSE"`
	CheckedType int     `json:"checkedType" gorm:"Column:checkedType"`
}

/**
 * @brief 获取心脏测量数据
 * @param db-数据库操作引擎
 * @param patientIds-患者ID集合
 * @return 病灶数据集合，错误信息
 */
func GetHeartExportData(db *gorm.DB, patientIds string) ([]*HeartMeasureInfo, error) {
	var nidus []*HeartMeasureInfo

	var query string
	query = "SELECT t1.patient_name, t1.check_id, t1.patient_id, t1.timeStamp, t1.sex, t1.age, t2.LVEF, a.imageNTPStamp, a.chamberType, a.chamberProb, "
	query += "a.longDiameter, a.area, a.volume, a.period"
	query += " FROM t_patient_info t1"
	query += " INNER JOIN t_cardiac_info t2 on t1.id=t2.patientId"
	query += " LEFT JOIN "
	query += " (SELECT t1.patientId, t1.imageNTPStamp, t2.type as chamberType, t2.chamberProb, t2.longDiameter, t2.shortDiameter, t2.area, t2.volume, t2.period"
	query += " FROM t_image_info t1 INNER JOIN t_chamber_info t2 ON t1.id=t2.imageId WHERE t1.imageType IN (4, 5)) AS a"
	query += " ON t1.id = a.patientId AND t2.chamberType=a.chamberType"
	query += " WHERE t1.isDelete=0 and t2.isDelete=0"
	//导出患者ID
	query += fmt.Sprintf(" AND t1.id in (%s)", patientIds)

	res := db.Raw(query).Scan(&nidus)
	if res.Error != nil {
		return nil, res.Error
	}

	return nidus, nil
}

func GetHeartMeasureData(db *gorm.DB, beginTime, endTime, tableName string) ([]*HeartMeasureData, error) {
	var list []*HeartMeasureData

	query := fmt.Sprintf("select t1.patientId, t2.type as chamberType, t2.checkedType, t1.LVEF, t1.LVEFSimpson from %v t1 left join t_chamber_info t2 ", tableName)
	query += "on t1.patientId = t2.patientId and t1.chamberType = t2.type where t2.checkedType > 0 and t2.isDelete = 0 "
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf("and t1.createTime >= '%v' and t1.createTime <= '%v' ", beginTime, endTime)
	}
	query += "order by t2.patientId, t2.type asc"

	fmt.Println(query)

	res := db.Raw(query).Scan(&list)

	if res.Error != nil {
		return nil, res.Error
	}
	return list, nil
}

func GetChamberMeasureData(db *gorm.DB, beginTime, endTime, tableName string) ([]*ChamberMeasureData, error) {
	var list []*ChamberMeasureData

	query := fmt.Sprintf("select t1.patientId, t1.type as chamberType, t1.checkedType, t1.aoDiameter, t1.ivst, t1.laDiameter, t1.lvDiameter, t1.lvpwt, t1.area, t1.padd, t1.period, t2.imageType from %v t1 ", tableName)
	query += "inner join t_image_info t2 on t1.imageId = t2.id "
	query += "where t1.checkedType > 0 and t1.isDelete = 0 and t1.period > 0 "
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf("and t1.createTime >= '%v' and t1.createTime <= '%v' ", beginTime, endTime)
	}
	query += "order by t1.patientId, t1.type, t2.imageType asc"

	res := db.Raw(query).Scan(&list)

	if res.Error != nil {
		return nil, res.Error
	}
	return list, nil
}

func GetWaveformMeasureData(db *gorm.DB, beginTime, endTime, tableName string) ([]*WaveformMeasureData, error) {
	var list []*WaveformMeasureData

	query := fmt.Sprintf("select patientId, checkedType, period, velE, velA, velep, TDIS, TAPSE from %v ", tableName)
	query += "where isDelete = 0 "
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf("and createTime >= '%v' and createTime <= '%v' ", beginTime, endTime)
	}
	query += "order by patientId, period asc"

	res := db.Raw(query).Scan(&list)

	if res.Error != nil {
		return nil, res.Error
	}
	return list, nil
}
